package com.jetinno.utils;

import java.io.IOException;
import java.io.OutputStream;

/**
 * Created by zhangyuncai on 2023/4/21.
 * 需要root才能使用该工具类
 */
public class ProcessUtils {

    public static final String SIMPLE_CMD_SCREEN_SHOT = "screencap -p /sdcard/sc.png";//截屏指定名称至指定路径
    public static final String SIMPLE_CMD_FORCE_STOP = "am force-stop com.jinuo.mhwang.jetinnocoffe  \n";//停止主APP

    public static void screenShot() {
        excuteCmd(SIMPLE_CMD_SCREEN_SHOT);
    }

    public static void forceStop() {
        excuteCmd(SIMPLE_CMD_FORCE_STOP);
    }

    public static void excuteCmd(String cmd) {
        Process process = null;
        OutputStream out = null;
        try {
            process = Runtime.getRuntime().exec("su");
            out = process.getOutputStream();
            try {
                out.write(cmd.getBytes());
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            process = null;
        }
    }

}
